package com.buddy.design.pattern._08adapter.loginadapter;

import com.buddy.design.pattern._08adapter.loginadapter.adapter.*;

public class PassportForThirdAdapter implements IPassportForThird {
    @Override
    public ResultMsg loginForQQ(String id) {
        return processLogin(id, LoginForQQAdapter.class);
    }

    @Override
    public ResultMsg loginForWeChart(String openId) {
        return processLogin(openId, LoginForWechartAdapter.class);
    }

    @Override
    public ResultMsg loginForWeiBo(String id) {
        return processLogin(id, LoginForWeiBoAdapter.class);
    }

    @Override
    public ResultMsg loginForToken(String token) {
        return processLogin(token, LoginForTokenAdapter.class);
    }

        private ResultMsg processLogin(String key,Class<? extends LoginAdapter> clazz){
//    private ResultMsg processLogin(String key, Class<?> clazz) {
        try {
            //适配器不一定要实现接口，因此使用Object
            LoginAdapter adapter = clazz.newInstance();
            if (adapter.support(adapter)) {
                return adapter.login(key, adapter);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
